package com.example.springmvc.demos.web;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;


@RestController
@RequestMapping("/user")
public class LoginController {
    /**
     * 检验用户的账号密码准确性
     * @param userName
     * @param password
     * @param session
     * @return
     */

    @RequestMapping("/login")
    public boolean login(String userName, String password, HttpSession session){
        //StringUtils.hasLength()是Spring提供的⼀个⼯具⽅法,判断字符串是否有值字符串为null或者""时,返回false,其他返回true

        if(!StringUtils.hasLength (userName)||!StringUtils.hasLength (password)){
            return false;
        }

        if(!"zhangsan".equals (userName)||!"88888888".equals (password)){
            return false;
        }

        session.setAttribute ("userName",userName);
        return true;

    }


    @RequestMapping("/getLogin")
    public String getLogin(HttpSession session){
        String userName = (String)session.getAttribute ("userName");
        if(StringUtils.hasLength ("userName")){
            return userName;
        }
        return"";
    }



}
